<template>
  <div>
    <div class="main">
      <div class="TopTitle">
        <h1>CAR BRANDS</h1>
        <form class="d-flex" role="search">
          <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search" v-model="inputVal"/>
          <button class="btn btn-outline-success" type="submit">Search</button>
        </form>
      </div>
      <div class="products">
        <div class="part" v-for="(item,index) in searchList" :key="index" @click="goDetails">
          <div class="pic">
            <img :src="item.pic" alt />
          </div>
          <div class="text">
            <h2>{{item.name}}</h2>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      inputVal:'',
      products: []
    };
  },
  mounted(){
      axios.get('https://mock.apifox.cn/m1/1255964-0-default/brands').then(res => (
        console.log(res.data),
        this.products = res.data
        )).catch(function (error) { // 请求失败处理
        console.log(error);
        });
  },
  methods:{
    goDetails(){
      this.$router.push({
        path:'/details'
      })
    }
  },
   computed:{
     searchList () {
      if (!this.inputVal) {
        return this.products
      }
      const data = []
      const reg = new RegExp(this.inputVal.toLowerCase())
      this.products.forEach(item => {
        if (reg.test(item.name.toLowerCase())) {
          data.push(item)
        }
      })
      return data
    }
  }
};
</script>
<style lang="scss" scoped>
.main {
  width: 96%;
  margin: auto;
  margin: 20px;
  .TopTitle {
    height: 60px;
    width: 60%;
    margin-left: 20px;
    display: flex;
    justify-content: space-between;
    border-bottom: 2px black solid;
    box-shadow: 10px 5px 5px black;
    color: #000000;
    input{
        height: 45px;
    }
    button{
        height: 45px;
        margin-right: 20px;
    }
  }
  .products {
    width: 100%;
    height: 800px;
    margin-top: 20px;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
        box-shadow: 0px 0px 5px black;

    .part {
      width: 12%;
      height: 320px;
      margin: 20px 40px;
      color: #000000;
      .pic {
        width: 96%;
        margin: auto;
        height: 180px;
        margin-top: 10px;
        border-radius: 10px;
        img {
          width: 100%;
          height: 180px;
          border-radius: 10px;
        }
      }
      .pic:hover{
        box-shadow: 0px 0px 5px black;
      }
      .text {
        margin-top: 30px;
      }
    }
  }
}
</style>